package com.xuecheng.content.api;

import com.xuecheng.base.exception.ValidationGroups;
import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.content.model.dto.AddCourseDto;
import com.xuecheng.content.model.dto.CourseBaseInfoDto;
import com.xuecheng.content.model.dto.EditCourseDto;
import com.xuecheng.content.model.dto.QueryCourseParamsDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.service.CourseBaseInfoService;
import com.xuecheng.content.util.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(value="课程信息管理接口",tags = "课程信息管理接口")
@RestController
@RequestMapping("/course")
public class CourseBaseInfoController {
    @Autowired
    private CourseBaseInfoService courseBaseInfoService;
    @ApiOperation("课程分页查询接口")
    @PreAuthorize("hasAuthority('xc_teachmanager_course_list')")//指定权限标识符号
    @PostMapping("/list")
    public PageResult<CourseBase> list(PageParams pageParams, @RequestBody(required = false) QueryCourseParamsDto queryCourseParamsDto){
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        Long companyId=null;
        if(StringUtils.isNotEmpty(user.getCompanyId())){
         companyId=Long.parseLong(user.getCompanyId());
        }
        PageResult<CourseBase> courseBasePageResult =
                courseBaseInfoService.queryCourseBaseList(companyId,pageParams, queryCourseParamsDto);
        return courseBasePageResult;
    }

    @ApiOperation(value = "新增课程接口")
    @PostMapping("")
    public CourseBaseInfoDto addCourseBase(@RequestBody @Validated(ValidationGroups.Inster.class) AddCourseDto addCourseDto){
        Long companyId=1232141425L;
        CourseBaseInfoDto courseBaseInfoDto = courseBaseInfoService.addCourseBase(companyId, addCourseDto);
        return courseBaseInfoDto;
    }

    @ApiOperation(value = "根据ID查询课程接口")
    @GetMapping("/{courseId}")
    public CourseBaseInfoDto getByCourseId(@PathVariable Long courseId){
//        //从JWT解析令牌的线程中取出当前用户的身份
//        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //使用SecurityUtil工具获取用户信息
        SecurityUtil.XcUser user = SecurityUtil.getUser();

        CourseBaseInfoDto courseBaseInfoDto = courseBaseInfoService.selectById(courseId);
        return courseBaseInfoDto;
    }

    @ApiOperation(value = "修改课程接口")
    @PutMapping("")
    public CourseBaseInfoDto updateCourse(@RequestBody @Validated(ValidationGroups.Update.class) EditCourseDto editCourseDto){
        //获取到用户所属机构的id
        Long companyId = 1232141425L;
        CourseBaseInfoDto courseBaseInfoDto =courseBaseInfoService.updateCourse(companyId,editCourseDto);
        return courseBaseInfoDto;
    }

}
